# -*- coding: utf-8 -*-

from flask import request, render_template
from loremipsum import get_sentence, get_sentences, get_paragraphs
                       
from itertools import izip
from datetime import datetime

from ...forms import NoteForm
from ..note import _proj_n


def add_note(lenta_id):
    if request.method == 'GET':
        return render_template('/lenta/note/add_note.html')
    else:
        form = NoteForm(request.form)
        if form.is_valid():
            ctx = dict(id=100500, namespace='ebanashka', lenta_id=100500)
            ctx.update(_proj_n(form.data))
            return render_template('/lenta/note/add_note_ok.html', note=ctx)
        return render_template('/lenta/note/add_note.html', errors=form.errors, note=form.data)


def view_note(namespace, lenta_id, note_id):
    """ """
    note = {
        'id': note_id,
        'title': get_sentence()[:100],
        'twit': ' '.join(get_sentences(3))[:140],
        'body': "\n \n".join(get_paragraphs(4)),
        'pubDate': datetime.today().replace(microsecond=0), 
        'tags': [t[:20] for t in get_sentences(5)],
        'namespace': namespace,
        'lenta_id': lenta_id
    }
    return render_template('/lenta/note/view_note.html', note=note)


def edit_note (note_id, lenta_id):
    if request.method == 'GET':
        note = {
            'id': note_id,
            'title': get_sentence()[:100],
            'twit': ' '.join(get_sentences(3))[:140],
            'body': "\n \n".join(get_paragraphs(4)),
            'pubDate': datetime.today().replace(microsecond=0), 
            'tags': [t[:20] for t in get_sentences(5)],
            'namespace': "ebanashka",
            'lenta_id': lenta_id
        }
        return render_template('/lenta/note/edit_note.html', note=NoteForm(note).htmlize())
    else:
        form = NoteForm(request.form)
        if form.is_valid():
            ctx = dict(id=100500, namespace='ebanashka', lenta_id=100500)
            ctx.update(_proj_n(form.cleaned_data))
            return render_template('/lenta/note/edit_note_ok.html', note=ctx)
        return render_template('/lenta/note/edit_note.html', errors=form.errors, note=form.data)


def delete_note (lenta_id, note_id):
    if request.method == 'GET':
        note = {
            'id': note_id,
            'title': get_sentence()[:100],
            'namespace': "ebanashka",
            'lenta_id': lenta_id
        }
        return render_template('/lenta/note/delete_note.html', note=note)
    else:
        return render_template('/lenta/note/delete_note_ok.html')
